package com.project.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.project.bean.BuyCartBean;
import com.project.bean.SaleCartBean;
import com.project.bean.StockShopBean;
import com.project.mapper.IBuyCartMapper;
import com.project.service.IBuyCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class BuyCartServiceImp implements IBuyCartService {
    @Autowired
    private IBuyCartMapper mapper;

    @Override
    public List<BuyCartBean> findById(Integer userId) {
        return mapper.findById(userId);
    }

    @Override
    public void update(Integer id, Integer price, Integer num) {
        BuyCartBean buyCartBean = mapper.selectById(id);
        buyCartBean.setBuyPrice(price);
        buyCartBean.setBuyNum(num);
        mapper.updateById(buyCartBean);
    }

    @Override
    public void del(Integer id) {
        mapper.deleteById(id);
    }

    @Override
    public void delByUserId(Integer userId) {
        QueryWrapper qw = new QueryWrapper<>();
        qw.eq("fk_userId",userId);
        mapper.delete(qw);
    }

    @Override
    public void add(StockShopBean stockShopBean, Integer userId, Integer num) {
        QueryWrapper qw = new QueryWrapper<>();
        qw.eq("fk_shopId",stockShopBean.getShopId());
        qw.eq("fk_userId",userId);
        BuyCartBean buyCartBean =mapper.selectOne(qw);
        if(buyCartBean==null){
            BuyCartBean buyCartBean1 = new BuyCartBean();
            buyCartBean1.setBuyNum(num);
            buyCartBean1.setStockShopId(stockShopBean.getShopId());
            buyCartBean1.setUserId(userId);
            //默认传入最新采购价，后面再修改
            buyCartBean1.setBuyPrice(stockShopBean.getNewBuyPrice());
            mapper.insert(buyCartBean1);
        }else {
            buyCartBean.setBuyNum(buyCartBean.getBuyNum()+1);
            mapper.updateById(buyCartBean);
        }
    }

}


